﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AudioCore.Biz;
using AudioCore.Entity;
using AudioCore.Common;
using AudioBookWS.ServiceUtil;
using AudioBook.AudioManagement.Util;

namespace AudioBook.AudioManagement.AudioManage
{
    public partial class EditArticle : System.Web.UI.Page
    {
        #region Fields & constants

        ArticleBiz bizArticle = new ArticleBiz();
        CategoryArticleBiz bizCategoryArticle = new CategoryArticleBiz();

        #endregion Fields & constants

        #region Private methods

        private void LoadData()
        {
            ArticleEntity enArticle = bizArticle.GetArticleByID(int.Parse(hidArticleID.Value));
            if (enArticle != null)
            {
                txtArticleName.Text = enArticle.ArticleName;
                txtArtist.Text = enArticle.Artist;
                txtAuthor.Text = enArticle.Author;
                txtDescription.Text = enArticle.Description;
                chkActive.Checked = enArticle.Actived;
                imgArticle.ImageUrl = string.Format(GUIConstants.WebPage.CommonPage.LoadImage, ((int)WSConstants.ContentType.Article).ToString(), enArticle.ArticleID, 0);
            }
            else
            {
                Response.Redirect(GUIConstants.WebPage.AudioPage.DefaultAudio);
            }
        }

        private int SaveArticle()
        {
            ArticleEntity enArticle = bizArticle.GetArticleByID(int.Parse(hidArticleID.Value));
            if (enArticle != null)
            {
                enArticle.ArticleName = txtArticleName.Text;
                enArticle.Description = txtDescription.Text;
                enArticle.HasImage = fulImage.HasFile;
                enArticle.Artist = txtArtist.Text;
                enArticle.Author = txtAuthor.Text;
                enArticle.LastTimeUpdate = DateTime.Now;
                enArticle.Actived = chkActive.Checked;

                int result = bizArticle.UpdateArticle(enArticle);

                if (fulImage.HasFile)
                {
                    string audioFolder = GlobalParam.DataFolder + Constant.AUDIO_FOLDER + enArticle.ArticleID + "\\";
                    if (!Directory.Exists(audioFolder))
                    {
                        Directory.CreateDirectory(audioFolder);
                    }
                    string imgFile = audioFolder + enArticle.ArticleID.ToString() + Constant.IMAGE_EXTENSION;
                    fulImage.SaveAs(imgFile);
                }
                return result;
            }
            else
            {
                valCommon.IsValid = false;
                valCommon.ErrorMessage = GUIConstants.ErrorMessage.TransactionError;
                return 0;
            }
        }

        #endregion

        #region Event handlers

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string idQuery = Request.QueryString[GUIConstants.Common.ID_QUERY];
                int articleID;
                if (!string.IsNullOrEmpty(idQuery) && int.TryParse(idQuery, out articleID))
                {
                    hidArticleID.Value = articleID.ToString();
                    LoadData();
                }
                else
                {
                    Response.Redirect(GUIConstants.WebPage.AudioPage.DefaultAudio);
                }
            }
        }

        protected void btnSaveAndClose_Click(object sender, EventArgs e)
        {
            int result = SaveArticle();
            if (result > 0)
            {
                Response.Redirect(string.Format(GUIConstants.WebPage.AudioPage.ArticleDetail, result));
            }
        }

        protected void btnSaveAndNew_Click(object sender, EventArgs e)
        {
            int result = SaveArticle();
            if (result > 0)
            {
                Response.Redirect(GUIConstants.WebPage.AudioPage.AddArticle);
            }
        }

        #endregion

    }
}
